package gui.dialogs;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import database.MySQLServer;
import database.SQLServer;

public class LoginDialog {
	private static MySQLServer server = null;
	
	private LoginDialog(){
	}
  
	/**
	 * Create a popup that asks details about the database server
	 * @return SQLServer instance
	 */
	static public SQLServer getServer(){		
		final Display display = new Display();
		final Shell shell = new Shell(display);
		
		shell.setLayout(new GridLayout(2, false));
	    GridData gridDataLabel = new GridData();
	    gridDataLabel.widthHint = 50;
	    gridDataLabel.grabExcessHorizontalSpace = true;
	    gridDataLabel.horizontalAlignment = GridData.FILL;	    
		
	    GridData gridDataText = new GridData();
	    gridDataText.widthHint = 125;
	    gridDataText.grabExcessHorizontalSpace = true;
	    gridDataText.horizontalAlignment = GridData.FILL;	
	    
		Label lhost = new Label(shell, SWT.SINGLE);
		lhost.setText ("Host");
		lhost.setLayoutData(gridDataLabel);		
		
		final Text thost = new Text(shell, SWT.SINGLE | SWT.BORDER);
		thost.setText("localhost");
		thost.setLayoutData(gridDataText);
		
		Label luser = new Label(shell, SWT.SINGLE);
		luser.setText ("User");
		luser.setLayoutData(gridDataLabel);
		  
		final Text tuser = new Text(shell, SWT.SINGLE | SWT.BORDER);
		tuser.setText("root");
		tuser.setLayoutData(gridDataText);
		
		Label lpass = new Label(shell, SWT.SINGLE);
		lpass.setText ("Pass");
		lpass.setLayoutData(gridDataLabel);	
		
		final Text tpass = new Text(shell, SWT.SINGLE | SWT.BORDER);
		tpass.setText("usbw");
		tpass.setLayoutData(gridDataText);
		
		Button accept = new Button(shell, SWT.PUSH);
		GridData acceptGridData = new GridData();
	  	acceptGridData.horizontalSpan = 2;
	  	acceptGridData.widthHint = 80;
	  	acceptGridData.grabExcessHorizontalSpace = true;
	  	acceptGridData.horizontalAlignment = GridData.END;	  
	  	accept.setText("OK");
	  	accept.setLayoutData(acceptGridData);
		accept.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {   
				server = new MySQLServer(thost.getText(),tuser.getText(),tpass.getText());
				if(server.testConnection()) {
					display.dispose();
				}
				else {
					String question = "De server accepteerd uw gebruikersnaam en wachtwoord niet. Wilt u opnieuw proberen in te loggen?";
					Confirm confirmDialog = new Confirm(shell, "Inloggen", question);
					if (!confirmDialog.open().accepted) {
						server = null;
						display.dispose();						
					}
				}
			}
		});
	  
		shell.pack();
		shell.open();	
		
		while (!shell.isDisposed ()) {
			if (!display.readAndDispatch ()) display.sleep ();
		}
		
		return server; 
	}
}